<?php

namespace app\model\v1\recruit;

use app\model\user\User;
use crmeb\traits\ModelTrait;
use think\Model;

class RecruitPersonageAuthentication extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
     //   'create_time','update_time','delete_time'
    ];


    public function user()
    {
        return $this->hasOne(User::class, 'uid', 'u_id');
    }






    /**
     * 求职招聘后台 - 人才认证管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecruitAuthentList($param = [])
    {
        $data["data"] = self::field("*,
            case when status=0 then '待审核'
            when status =1 then '通过'
            when status =2 then '拒绝'
            end as status_text
            ")
            ->with([
                'user'=> function($query) {
                    $query->field("*,
            case when user_type='routine' then '小程序'
            when user_type ='h5' then 'H5'
            when user_type ='app' then 'APP'
            end as user_type
            ");
                },
            ])
            ->whereNULL("delete_at")
            ->where(function ($query) use ($param){
                if(!empty($param["word"])) $query->where("user_name", 'like', '%'.$param["word"].'%');   //根据用户名称搜索
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],$wheretime[1]]);
                }
            })
            ->order("id", "desc")
            ->page($param["page"], $param["limit"])
            ->select()
            ->toArray();
//        foreach ($data["data"] as &$item) {
//            //用户类型
//            if ($item['user']['user_type'] == 'routine') {
//                $item['user']['user_type'] = '小程序';
//            } else if ($item['user']['user_type'] == 'wechat') {
//                $item['user']['user_type'] = '公众号';
//            } else if ($item['user']['user_type'] == 'h5') {
//                $item['user']['user_type'] = 'H5';
//            } else if ($item['user']['user_type'] == 'pc') {
//                $item['user']['user_type'] = 'PC';
//            } else if ($item['user']['user_type'] == 'app') {
//                $item['user']['user_type'] = 'APP';
//            } else $item['user']['user_type'] = '其他';
//            if ($item['user']['sex'] == 1) {
//                $item['user']['sex'] = '男';
//            } else if ($item['user']['sex'] == 2) {
//                $item['user']['sex'] = '女';
//            } else $item['user']['sex'] = '保密';
//        }

        $data["count"] = self::whereNULL("delete_at")
            ->where(function ($query) use ($param){
                if(!empty($param["word"])) $query->where("user_name", 'like', '%'.$param["word"].'%');   //根据用户名称搜索
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [$wheretime[0],$wheretime[1]]);
                }
            })
            ->count();

        return $data;
    }



    public static function getRecruitAuthentDetail($param = [])
    {
        $detail = self::field("*,
            case when status=0 then '待审核'
            when status =1 then '通过'
            when status =2 then '拒绝'
            end as status_text
            ")
            ->where("id", $param["id"])
            ->find();

        return $detail;
    }


    /**
     * 求职招聘后台 - 人才认证管理列表 - 审核按钮
     * @param $param
     * @return RecruitCompany
     */
    public static function enableRecruitAuthent($param = [])
    {
        $update = ['status' => $param["status"]];
        if(!empty($param["refuse_reason"])) $update["refuse_reason"] = $param["refuse_reason"];

        $res = self::where("id", $param['id'])->update($update);
        return $res;
    }


    public static function getUserIsAuthent($uid = '')
    {
        $find = self::where(["u_id"=>$uid])->where("status", 1)->whereNull("delete_at")->find();
        if(!empty($find)) return 1;
        return 2;
    }

}